package com.dataStructures.queue;

/**
 * 用数组模拟队列
 */
public class ArrayQueueDemo {

	public static void main(String[] args) {
		ArrayQueue queue = new ArrayQueue(3);
		queue.add(1);
		queue.add(2);
		queue.add(3);
		queue.add(4);
		queue.show();
		queue.get();
		queue.get();
		queue.get();
		queue.get();

	}
}

class ArrayQueue {
	private int front;
	private int rear;
	private final int maxSize;
	private final int[] arr;

	public ArrayQueue(int maxSize) {
		this.maxSize = maxSize;
		this.arr = new int[maxSize];
		this.front = -1;
		this.rear = -1;
	}

	public boolean isFull() {
		return rear == maxSize - 1;
	}

	public boolean isEmpty() {
		return front == rear;
	}

	public void add(int n) {
		if (isFull()) {
			System.out.println("queue full");
			return;
		}
		arr[++rear] = n;
	}

	public int get() {
		if (isEmpty()) {
			throw new RuntimeException("queue empty");
		}
		return arr[++front];
	}

	public void show() {
		if (isEmpty()) {
			throw new RuntimeException("queue empty");
		}
		for (int n : arr) {
			System.out.printf("%d\n", n);
		}
	}

	public int getHead() {
		if (isEmpty()) {
			throw new RuntimeException("queue empty");
		}
		return arr[front + 1];
	}

}
